/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package servlets;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Vector;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import presenter.Presenter;

/**
 *
 * @author root
 */
public class ConnectedServlet extends HttpServlet {
    
    public static Vector<HttpSession> sessions = new Vector<HttpSession>();
    
    private boolean error = true;
    private static final String ERROR = "Wrong login or password";
    
    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
     * methods and creates sessions.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        
        response.setContentType("text/html;charset=UTF-8");

        String login = request.getParameter("login");
        String password = request.getParameter("pwd");
        Map<String, String> Onglets = new HashMap<String,String>();
        Map<String, String> conversation;
        ArrayList<String> newDemande;
        ArrayList<String> newMessage = new ArrayList<>();
        ArrayList<String> demandeAccepte = new ArrayList<String>();
        
        error = !(Presenter.checkProfile(login, password));
        if (!error){
            HttpSession session = request.getSession();
            conversation = Presenter.getUnreadMsg(login);
            conversation.put("Home", "");
            Onglets = conversation;
            session.setAttribute("login", login);
            session.setAttribute("password", password);
            session.setAttribute("isConnected", true);
            Presenter.setStatus(login, "Connected");
            session.setAttribute("Onglets", Onglets);
            session.setAttribute("ongletActif","Home");
            newDemande = Presenter.getContactDemand(login);
            session.setAttribute("demandeContact", newDemande);
            session.setAttribute("newMessage", newMessage);
            session.setAttribute("demandeAccepte", demandeAccepte);
            session.setAttribute("conversation", conversation);
            session.setAttribute("contacts", Presenter.getContactList(login));
            session.setAttribute("messageToSend", "");
            session.setAttribute("refreshed", "false");
            request.setAttribute("waitingFile", Presenter.getWaitingFile(login));
            session.setAttribute("scrollY", "0");
            session.setAttribute("caretPos", "0");
            request.setAttribute("thisSession", session);
            if (!sessions.contains(session)) {
                sessions.add(session);
            }
            
            //request.setAttribute("contacts", Presenter.getContactList(login));
        }
        else {
            request.setAttribute("error", ERROR);
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
        doPost(request, response);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
        if (error) {
            this.getServletContext().getRequestDispatcher("/WEB-INF/connection.jsp").forward(request, response);
        }
        else {     
        //Pour pouvoir afficher les sessions en cours
            //request.setAttribute("sessions", this.sessions);
            this.getServletContext().getRequestDispatcher("/WEB-INF/connected.jsp").forward(request, response);
        }
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
